package com.geovis.emergency.spd.biz.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.core.exception.BizException;
import com.geovis.emergency.spd.biz.system.mapper.SystemAdminUserMapper;
import com.geovis.emergency.spd.biz.system.service.ISystemAdminUserService;
import com.geovis.emergency.spd.biz.system.service.ISystemMainRoleService;
import com.geovis.emergency.spd.biz.system.service.ISystemUserRoleService;
import com.geovis.emergency.spd.biz.system.service.ISystemUserService;
import com.geovis.emergency.spd.entity.system.entity.SystemAdminUser;
import com.geovis.emergency.spd.entity.system.entity.SystemMainRole;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.entity.system.entity.SystemUserRole;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemAdminUserSaveDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class SystemAdminUserServiceImpl extends ServiceImpl<SystemAdminUserMapper, SystemAdminUser> implements ISystemAdminUserService {

    private final ISystemUserRoleService systemUserRoleService;
    private final ISystemUserService systemUserService;
    private final ISystemMainRoleService systemMainRoleService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SystemAdminUserSaveDTO dto) {
        long count=count(Wrappers.lambdaUpdate(SystemAdminUser.class)
                .eq(SystemAdminUser::getRealName, dto.getRealName())
                .eq(SystemAdminUser::getPhone, dto.getPhone()));
        if(count>0)
        {
            throw new BizException("用户已存在！");
        }
        SystemAdminUser entity= BeanUtil.toBean(dto,SystemAdminUser.class);
        baseMapper.insert(entity);
        SystemUser user=systemUserService.getOne(Wrappers.lambdaUpdate(SystemUser.class)
                .eq(SystemUser::getRealName, dto.getRealName())
                .eq(SystemUser::getPhone, dto.getPhone()));
        if(user==null)
        {
            throw new BizException("该用户不存在，请核实姓名和手机号！");
        }
        List<SystemUserRole> userRoles=systemUserRoleService.list(Wrappers.lambdaUpdate(SystemUserRole.class)
                .eq(SystemUserRole::getUserId, user.getId()));
        //这里新角色逻辑后，这里应该只有一条记录
        if(!CollectionUtils.isEmpty(userRoles))
        {

            updateSystemUserRole(userRoles.get(0),"admin");
        }

    }

    private String getRoleIdByName(String name)
    {
        return systemMainRoleService.getOne(Wrappers.lambdaUpdate(SystemMainRole.class)
                .eq(SystemMainRole::getEnName, name)).getId();
    }

    @Override
    public List<SystemAdminUser> list(String realName, String phone) {

        return list(Wrappers.lambdaUpdate(SystemAdminUser.class)
                .like(StringUtils.hasText(realName),SystemAdminUser::getRealName, realName)
                .like(StringUtils.hasText(phone),SystemAdminUser::getPhone, phone));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String id) {
        SystemAdminUser adminUser=baseMapper.selectById(id);
        SystemUser user=systemUserService.getOne(Wrappers.lambdaUpdate(SystemUser.class)
                .eq(SystemUser::getRealName, adminUser.getRealName())
                .eq(SystemUser::getPhone, adminUser.getPhone()));
        baseMapper.deleteById(id);

        String adminRoleId=getRoleIdByName("admin");
        SystemUserRole userRole=systemUserRoleService.getOne(Wrappers.lambdaUpdate(SystemUserRole.class)
                .eq(SystemUserRole::getUserId, user.getId())
                .eq(SystemUserRole::getRoleId, adminRoleId));

        if(userRole!=null)
        {
            //更新用户为普通用户
            updateSystemUserRole(userRole,"normal");
        }
    }

    private void updateSystemUserRole(SystemUserRole userRole,String type)
    {
        SystemUserRole newUserRole=BeanUtil.toBean(userRole,SystemUserRole.class);
        newUserRole.setRoleId(getRoleIdByName(type));
        systemUserRoleService.updateById(newUserRole);
    }
}
